package com.yichenyang.ordersystem.controller;

import com.yichenyang.ordersystem.entity.*;
import com.yichenyang.ordersystem.service.AdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CopyOnWriteArrayList;

@Controller
@RequestMapping("admin")
public class AdminController {

    @Autowired
    private AdminService adminService;

    private int pageSize = 10;
    private int pageCount = 0;
    private int pageTotal;

    @RequestMapping("list")
    public String memberList(HttpSession session) {
        session.setAttribute("page", 1);
        int adminsAll = adminService.queryAll();
        session.setAttribute("adminNum", adminsAll);
        session.setAttribute("adminPages", getPages());

        List<AdminTemp> admins = adminService.queryAllByLimit(0, pageSize);
        session.setAttribute("admins", admins);
        return "pages/admin/list::adminList";
    }

    @PostMapping("add")
    @ResponseBody
    public int memberAdd(@RequestBody Admin admin) {
        admin.setStatus("1");
        admin = adminService.insert(admin);
        return admin.getId();
    }

    @PostMapping("find")
    public String memberFind(@RequestBody Map<String, String> map, final HttpSession session) {
        if (session.getAttribute("admins") != null) {
            List<AdminTemp> adminTemps = adminService.queryByConditions(map.get("username"), map.get("start"), map.get("end"));
            session.setAttribute("admins", adminTemps);
        }
        return "pages/admin/list::adminList";
    }

    //换页
    @PostMapping("page/{page}")
    public String changePage(@PathVariable int page, HttpSession session) {
        pageCount = (page-1)*10;
        List<AdminTemp> adminTemps = adminService.queryAllByLimit(pageCount, pageSize);
        session.setAttribute("admins",adminTemps);
        return "pages/admin/list::adminList";
    }

    //启用用户
    @GetMapping("invocation/{id}")
    @ResponseBody
    public void invocation(@PathVariable int id) {
        adminService.updateStatus("1",id);
    }

    //停用用户
    @GetMapping("stop/{id}")
    @ResponseBody
    public void stop(@PathVariable int id) {
        adminService.updateStatus("0",id);
    }

    //删除信息
    @GetMapping("del/{id}")
    public String del(@PathVariable int id, HttpSession session) {
        adminService.deleteById(id);
        List<AdminTemp> admins = adminService.queryAllByLimit(pageCount, pageSize);
        session.setAttribute("admins",admins);
        return "pages/admin/list::adminList";
    }

    @GetMapping("getId/{id}")
    @ResponseBody
    public int getId(@PathVariable int id) {
        Admin admin = adminService.queryById(id);
        return admin.getRoleId();
    }

    //修改角色信息
    @PostMapping("update/{id}")
    @ResponseBody
    public void update(@PathVariable int id,@RequestBody Admin admin) {
        admin.setId(id);
        adminService.update(admin);
    }

    public List<Integer> getPages() {
        int adminsAll = adminService.queryAll();
        pageTotal = adminsAll / pageSize + (adminsAll % pageSize == 0 ? 0:1);
        List<Integer> pages = new CopyOnWriteArrayList<>();
        for (int i = 0; i < pageTotal; i++) {
            pages.add(i+1);
        }
        return pages;
    }

}
